﻿using Kingdee.BOS.WebApi.DataEntities;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Windows.Interop;
using WebApi_SY.Entity;
using WebApi_SY.Models;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace WebApi_SY.Controllers
{
    public class sli_workorderController : ApiController
    {
        public sli_workorderController()
        {
            // _context = context;

        }
        [System.Web.Http.HttpPost]
        public async Task<object> Insert([Microsoft.AspNetCore.Mvc.FromBody] sli_work_order model)
        {
            try
            {
                var context = new YourDbContext();
                var header = new sli_work_order
                {
                    Fbillno = model.Fbillno,
                    Forderno= model.Forderno,
                    Fnotes = model.Fnotes,
                    Fdate = model.Fdate,
                    Fqty = model.Fqty,
                    Fweight = model.Fweight,
                    Fplanstart = model.Fplanstart,
                    Fplanend = model.Fplanend,
                    Fordertype = model.Fordertype,
                    
                    sli_work_orderEntry = model.sli_work_orderEntry.Select(d => new sli_work_orderEntry
                    {
                        Id = model.Id,
                        Fseq = d.Fseq,
                        Forderentryid = d.Forderentryid,
                        Forderid = d.Forderid,
                        Fworkorderlistid = d.Fworkorderlistid,
                        Fqty = d.Fqty

                    }).ToList()
                };



                context.Sli_work_order.Add(header);
                await context.SaveChangesAsync();
                var dataNull = new
                {
                    code = 200,
                    msg = "Success",
                    modelid = header.Id,
                    Date = header.Id.ToString() + "保存成功"

                };
                return dataNull;
            }
            catch (Exception ex)
            {
                return JsonConvert.SerializeObject(ex.ToString());
            }
        }
        /// <summary>
        /// 插入选择的数据，生成一个新的工令号，并删除原有的工令号下对应的行
        /// </summary>
        /// <param name="model">选择的行数据及表头数据</param>
        /// <returns></returns>
        [System.Web.Http.HttpPost]
        public async Task<object> Insert_Delete([Microsoft.AspNetCore.Mvc.FromBody] sli_work_order model)
        {
            using (var context = new YourDbContext())
            using (var transaction = await context.Database.BeginTransactionAsync())
            {
                try
                {
                    //------------------- 1. 删除旧数据（表体 + 可能关联的表头）-------------------
                    if (model.sli_work_orderEntry != null && model.sli_work_orderEntry.Any())
                    {
                        // 获取所有涉及的 Fworkorderlistid
                        var workorderlistIds = model.sli_work_orderEntry
                            .Select(e => e.Fworkorderlistid)
                            .Distinct()
                            .ToList();

                        // 查找所有关联的表体数据
                        var existingEntries = context.Sli_work_orderEntry
                            .Where(e => workorderlistIds.Contains(e.Fworkorderlistid))
                            .ToList();



                        if (existingEntries.Any())
                        {
                            // 记录这些表体数据关联的表头ID
                            var headerIdsToCheck = existingEntries
                                .Select(e => e.Id)
                                .Distinct()
                                .ToList();

                            // 先删除表体数据
                            context.Sli_work_orderEntry.RemoveRange(existingEntries);
                             context.SaveChanges();

                            // 检查哪些表头已经没有表体数据了，并删除
                            var headersToDelete = context.Sli_work_order
                                .Where(h => headerIdsToCheck.Contains(h.Id) &&
                                           !context.Sli_work_orderEntry.Any(e => e.Id == h.Id))
                                .ToList();

                            if (headersToDelete.Any())
                            {
                                context.Sli_work_order.RemoveRange(headersToDelete);
                                await context.SaveChangesAsync();
                            }
                        }
                    }

                    //------------------- 2. 插入新数据（表头 + 表体）-------------------
                    var header = new sli_work_order
                    {
                        Fbillno = model.Fbillno,
                        Fname = model.Fname,
                        Forderno = model.Forderno,
                        Fnotes = model.Fnotes,
                        Fdate = model.Fdate,
                        Fqty = model.Fqty,
                        Fforgeqty = model.Fforgeqty,
                        Fforgeweight = model.Fforgeweight,
                        Fweight = model.Fweight,
                        Fplanstart = model.Fplanstart,
                        Fplanend = model.Fplanend,
                        Fordertype = model.Fordertype,
                        Fslimetal = model.Fslimetal,
                        Fslidrawingno = model.Fslidrawingno,
                        Fsliexplanation = model.Fsliexplanation,
                        Fsliheattreatment = model.Fsliheattreatment,
                        Fmeterialid = model.Fmeterialid,
                        Fdescription = model.Fdescription,

                        sli_work_orderEntry = model.sli_work_orderEntry.Select(d => new sli_work_orderEntry
                        {
                            Id = model.Id,
                            Fseq = d.Fseq,
                            Forderentryid = d.Forderentryid,
                            Forderid = d.Forderid,
                            Fworkorderlistid = d.Fworkorderlistid,
                            Fqty = d.Fqty

                        }).ToList()
                    };

                    context.Sli_work_order.Add(header);
                    await context.SaveChangesAsync();

                    // 提交事务
                    await transaction.CommitAsync();

                    return new
                    {
                        code = 200,
                        msg = "Success",
                        modelid = header.Id,
                        Date = $"{header.Id} 保存成功"
                    };
                }
                catch (Exception ex)
                {
                    await transaction.RollbackAsync();
                    return new
                    {
                        code = 500,
                        msg = "Error",
                        error = ex.Message
                    };
                }
            }




            //try
            //{
            //    //-------------------直接插入数据-------------------
            //    var context = new YourDbContext();
                
            //    context.Sli_work_order.Add(header);
            //    await context.SaveChangesAsync();

            //    ///-------------------删除原有的工令号下对应的行-------------------




            //    var dataNull = new
            //    {
            //        code = 200,
            //        msg = "Success",
            //        modelid = header.Id,
            //        Date = header.Id.ToString() + "保存成功"

            //    };
            //    return dataNull;
            //}
            //catch (Exception ex)
            //{
            //    return JsonConvert.SerializeObject(ex.ToString());
            //}
        }



        [System.Web.Http.HttpGet]
      public IHttpActionResult GetHeaderWorkorder(
      int page = 1,
      int pageSize = 10,
      string FbillNo = null,
      string Fname = null,
      string Fdesciption = null,
      string Fslidrawingno = null,
      DateTime? Fdatefrom = null,
      DateTime? Fdateto = null,
      string FOrderType = null,
      int Fstatus =1)
        {
            var context = new YourDbContext();
            IQueryable<sli_work_order> query = context.Sli_work_order;

            // Apply filters based on provided parameters

            if (Fdatefrom.HasValue)
            {
                query = query.Where(q => q.Fdate >= Fdatefrom.Value);
            }
            if (!string.IsNullOrEmpty(FbillNo))
            {
                query = query.Where(q => q.Fbillno.Contains(FbillNo));
            }

            if (!string.IsNullOrEmpty(Fname))
            {
                query = query.Where(q => q.Fname.Contains(Fname));
            }

            if (!string.IsNullOrEmpty(Fdesciption))
            {
                query = query.Where(q => q.Fslidrawingno.Contains(Fdesciption));
            }

            if (!string.IsNullOrEmpty(Fslidrawingno))
            {
                query = query.Where(q => q.Fslidrawingno.Contains(Fslidrawingno));
            }

            if (Fdatefrom.HasValue)
            {
                query = query.Where(q => q.Fdate >= Fdatefrom.Value);
            }

            if (Fdateto.HasValue)
            {
                query = query.Where(q => q.Fdate <= Fdateto.Value);
            }

            if (!string.IsNullOrEmpty(FOrderType))
            {
                query = query.Where(q => q.Fordertype == FOrderType);
            }

        

            var totalCount = query.Count();
            var totalPages = (int)Math.Ceiling((double)totalCount / pageSize);
            var paginatedQuery = query.OrderByDescending(b => b.Id).Skip((page - 1) * pageSize).Take(pageSize);

            var result = paginatedQuery.Select(a => new
            {
                id = a.Id,
                Fname = a.Fname,
                Fmeterialid = a.Fmeterialid,
                Fslimetal = a.Fslimetal,
                Fdescription = a.Fdescription,
                Fslidrawingno = a.Fslidrawingno,
                Fsliheattreatment = a.Fsliheattreatment,
                Fsliexplanation = a.Fsliexplanation,
                Fbillno = a.Fbillno,
                Fdate = a.Fdate,
                Fqty = a.Fqty,
                Fweight = a.Fweight,
                Fplanstart = a.Fplanstart,
                Fplanend = a.Fplanend,
                Fordertype = a.Fordertype,
                Fforgeqty = a.Fforgeqty,
                Fforgeweight = a.Fforgeweight,
                Fstatus = a.Fstatus

            });

            var response = new
            {
                code = 200,
                msg = "OK",
                data = new
                {
                    totalCounts = totalCount,
                    totalPagess = totalPages,
                    currentPages = page,
                    pageSizes = pageSize,
                    data = result
                }
            };

            return Ok(response);
        }
        public IHttpActionResult GetTableWorkorder(int page = 1, int pageSize = 10)
        {
            var context = new YourDbContext();

            var query = context.Sli_work_order.Include(a => a.sli_work_orderEntry);


            //var query = from p in context.Sli_work_order
            //            join c in context.Sli_work_orderEntry on p.Id equals c.Id
            //            select new
            //            {
            //                Sli_work_order = p,
            //                Sli_work_orderEntry = c
            //            };

            var totalCount = query.Count();
            var totalPages = (int)Math.Ceiling((double)totalCount / pageSize);
            var paginatedQuery = query.OrderByDescending(b => b.Id).Skip((page - 1) * pageSize).Take(pageSize);
            var result = paginatedQuery.Select(a => new
            {
                id = a.Id,
                Fbillno = a.Fbillno,
                Forderno = a.Forderno,
                Fnotes = a.Fnotes,
                Fdate = a.Fdate,
                Fqty = a.Fqty,
                Fweight = a.Fweight,
                Fplanstart = a.Fplanstart,
                Fplanend = a.Fplanend,
                Fordertype = a.Fordertype,
                Sli_plan_modelEntry = a.sli_work_orderEntry.Select(b => new
                {
                    Fentryid = b.Fentryid,
                    Id = b.Id,
                    Fworkorderlistid = b.Fworkorderlistid,
                    Fseq = b.Fseq,
                    Fqty = b.Fqty,
                    Fcommitqty = b.Fcommitqty,
                    Forderid = b.Forderid,
                    Forderentryid = b.Forderentryid,
                    Fstatus = b.Fstatus,
                    Fclosed = b.Fclosed,
                })

            });
            var response = new    // 定义 前端返回数据  总记录，总页，当前页 ，size,返回记录
            {
                code = 200,
                msg = "OK",
                data = new
                {
                    totalCounts = totalCount,
                    totalPagess = totalPages,
                    currentPages = page,
                    pageSizes = pageSize,
                    data = result
                }


            };

            return Ok(response);
        }

        [HttpPut]
        public IHttpActionResult UpdateWorkorder( [FromBody] sli_work_order model)
        {
            if (!ModelState.IsValid)
            {
                return base.BadRequest(ModelState);
            }

            using (var context = new YourDbContext())
            {
                //var workOrder = context.Sli_work_order .Include(a => a.sli_work_orderEntry);

                var workOrder = context.Sli_work_order.FirstOrDefault(p => p.Id == model.Id);
                var Sli_work_orderEntrys = context.Sli_work_orderEntry.Where(p => p.Id == model.Id).ToList();

                if (workOrder == null)
                {
                    return NotFound();
                }



                //// 确保子表集合不为 null
                //if (workOrder.sli_work_orderEntry == null)
                //{
                //    workOrder.sli_work_orderEntry = new List<sli_work_orderEntry>();
                //}

                // 更新主表字段（允许空值）
                workOrder.Fbillno = model.Fbillno;
                workOrder.Forderno = model.Forderno;
                workOrder.Fdate = model.Fdate;
                workOrder.Fqty = model.Fqty;
                workOrder.Fweight = model.Fweight;
                workOrder.Fplanstart = model.Fplanstart;
                workOrder.Fplanend = model.Fplanend;
                workOrder.Fordertype = model.Fordertype;  //Fsliexplanation
                workOrder.Fsliexplanation = model.Fsliexplanation;  //Fsliexplanation

                // 更新子表数据（全量替换）
                if (model.sli_work_orderEntry != null)
                {
                    context.Sli_work_orderEntry.RemoveRange(Sli_work_orderEntrys);
                    context.SaveChanges();
                    workOrder.sli_work_orderEntry = model.sli_work_orderEntry
                        .Select(e => new sli_work_orderEntry
                        {
                            Id = e.Id,
                            Fseq = e.Fseq, // Keep original sequence instead of index+1
                            Fqty = e.Fqty,
                            Fcommitqty = e.Fcommitqty,
                            Forderid = e.Forderid,
                            Forderentryid = e.Forderentryid,
                            Fstatus = e.Fstatus,
                            Fclosed = e.Fclosed,
                            Fworkorderlistid = e.Fworkorderlistid
                        }).ToList();
                }

                // 生成 Fnotes：根据子表关联视图拼接 Fproductno
                var fworkorderlistIds = workOrder.sli_work_orderEntry
                    .Where(entry => entry.Fworkorderlistid != null)
                    .Select(entry => entry.Fworkorderlistid.Value)
                    .Distinct()
                    .ToList();

                List<string> productNos = new List<string>();
                if (fworkorderlistIds.Any())
                {
                    productNos = context.sli_workorderlist_view
                        .Where(v => fworkorderlistIds.Contains(v.Fworkorderlistid) && v.Fproductno != null)
                        .Select(v => v.Fproductno)
                        .Distinct()
                        .ToList();
                }

                workOrder.Fnotes = productNos.Any() ? string.Join(";", productNos) : string.Empty;

                context.SaveChanges();

                // 返回更新后的数据
                //var updatedData = new
                //{
                //    id = workOrder.Id,
                //    Fbillno = workOrder.Fbillno,
                //    Forderno = workOrder.Forderno,
                //    Fnotes = workOrder.Fnotes,
                //    Fdate = workOrder.Fdate,
                //    Fqty = workOrder.Fqty,
                //    Fweight = workOrder.Fweight,
                //    Fplanstart = workOrder.Fplanstart,
                //    Fplanend = workOrder.Fplanend,
                //    Fordertype = workOrder.Fordertype,
                //    Sli_plan_modelEntry = workOrder.sli_work_orderEntry.Select(b => new
                //    {
                //        Fentryid = b.Fentryid,
                //        Id = b.Id,
                //        Fworkorderlistid = b.Fworkorderlistid,
                //        Fseq = b.Fseq,
                //        Fqty = b.Fqty,
                //        Fcommitqty = b.Fcommitqty,
                //        Forderid = b.Forderid,
                //        Forderentryid = b.Forderentryid,
                //        Fstatus = b.Fstatus,
                //        Fclosed = b.Fclosed
                //    })
                //};

                return Ok(new { code = 200, msg = "更新成功", data = "更新成功" });
            }
        }



        /// <summary>
        /// 主表更新模型
        /// </summary>
        public class WorkorderUpdateModel
        {
            public string Fbillno { get; set; }
            public string Forderno { get; set; }
            public string Fnotes { get; set; }
            public DateTime Fdate { get; set; }
            public int Fqty { get; set; }
            public decimal Fweight { get; set; }
            public DateTime Fplanstart { get; set; }
            public DateTime Fplanend { get; set; }
            public string Fordertype { get; set; }

            /// <summary>
            /// 子表条目（全量更新）
            /// </summary>
            public List<EntryUpdateModel> Entries { get; set; }
        }

        /// <summary>
        /// 子表更新模型
        /// </summary>
        public class EntryUpdateModel
        {
            public int Fentryid { get; set; }
            public int Fseq { get; set; }
            public int Fqty { get; set; }
            public int Fcommitqty { get; set; }
            public int Forderid { get; set; }
            public int Forderentryid { get; set; }
            public int Fstatus { get; set; }
            public int Fclosed { get; set; }
            public int? Fworkorderlistid { get; internal set; }
        }



        public IHttpActionResult GetTableWorkorderbyid(int? id = null)
        {
            var context = new YourDbContext();

            var query = context.Sli_work_order_view1.Include(a => a.sli_work_orderEntry_view);

            if (id.HasValue)
            {
                query = query.Where(t => t.Id == id.Value);
            }

            var paginatedItem = query
                .Select(a => new
                {
                    id = a.Id,
                    Fbillno = a.Fbillno,
                    Forderno = a.Forderno,
                    Fnotes = a.Fnotes,
                    Fdate = a.Fdate,
                    Fqty = a.Fqty,
                    Fweight = a.Fweight,
                    Fplanstart = a.Fplanstart,
                    Fplanend = a.Fplanend,
                    Fordertype = a.Fordertype,
                    Fforgeqty = a.Fforgeqty,
                    Fforgeweight = a.Fforgeweight,
                    Fslimetal = a.Fslimetal,
                    Fdescription = a.Fdescription,
                    Fslidrawingno = a.Fslidrawingno,
                    Fsliheattreatment = a.Fsliheattreatment,
                    Fsliexplanation = a.Fsliexplanation,
                    Fname = a.Fname,
                    Sli_plan_modelEntry = a.sli_work_orderEntry_view.OrderBy(b => b.Fseq).Select(b => new
                    {
                        Fentryid = b.Fentryid,
                        Id = b.Id,
                        Fworkorderlistid = b.Fworkorderlistid,
                        Fseq = b.Fseq,
                        Fqty = b.Fqty,
                        Fworkweight = b.Fworkweight,
                        Fsplittype = b.Fsplittype,
                        Fname = b.Fname,
                        Fdescription = b.Fdescription,
                        Forderid = b.Forderid,
                        Forderentryid = b.Forderentryid,
                        Fproductno = b.Fproductno
                    })
                })
                .FirstOrDefault();

                var response = new
                {
                    code = 200,
                    msg = "OK",
                    data = new
                    {
                        data = paginatedItem != null ? new[] { paginatedItem } : new object[0]
                    }
                };
                return Ok(response);
        }

        [Microsoft.AspNetCore.Mvc.HttpGet]
        public IHttpActionResult GetTableprocess(int page = 1, int pageSize = 10,int ? id=null)
        {
            var context = new YourDbContext();

            IQueryable<sli_work_orderprocess_view> query = context.Sli_work_orderprocess_view;
            //var query = from p in context.Sli_work_order
            //            join c in context.Sli_work_orderEntry on p.Id equals c.Id
            //            select new
            //            {
            //                Sli_work_order = p,
            //                Sli_work_orderEntry = c
            //            };
            if (id.HasValue)
            {
                query = query.Where(t => t.Id == id.Value);
            }
            var totalCount = query.Count();
            var totalPages = (int)Math.Ceiling((double)totalCount / pageSize);
            var paginatedQuery = query.OrderByDescending(b => b.Id).Skip((page - 1) * pageSize).Take(pageSize);
            
            var response = new    // 定义 前端返回数据  总记录，总页，当前页 ，size,返回记录
            {
                code = 200,
                msg = "OK",
                data = new
                {
                    totalCounts = totalCount,
                    totalPagess = totalPages,
                    currentPages = page,
                    pageSizes = pageSize,
                    data = paginatedQuery
                }


            };

            return Ok(response);
        }


        [System.Web.Http.HttpPost]
        public async Task<object> sli_workorder_entry_Delete(List<int> id)
        {
            try
            {
                var context = new YourDbContext();
                var headersToDelete = context.Sli_work_orderEntry.Where(h => id.Contains(h.Fentryid)).ToList();
                if (headersToDelete == null)
                {
                    var dataNull = new
                    {
                        code = 200,
                        msg = "ok",
                        orderId = id.ToString(),
                        date = id.ToString() + "不存在"
                    };
                    //string json = JsonConvert.SerializeObject(data);
                    return dataNull;
                }
                //context.Sli_work_orderEntry.RemoveRange(headersToDelete);

                foreach (var DeleteID in id)
                {
                    var Sli_witnessing_orderbill = context.Sli_work_orderEntry.Where(b => b.Fentryid == DeleteID);
                    context.Sli_work_orderEntry.RemoveRange(Sli_witnessing_orderbill);
                }

                await context.SaveChangesAsync();
                
                
                // var data = new { Status = "Success", Message = "Data retrieved successfully", Data = new { /* actual data here */ } };
                var data = new
                {
                    code = 200,
                    msg = "Success",
                    orderId = id.ToString(),
                    date = id.ToString() + "删除成功"
                };
                return data;
            }
            catch (Exception ex)
            {
                var data = new
                {
                    code = 400,
                    msg = "失败",
                    orderId = id.ToString(),
                    date = ex.ToString()
                };
                return data;
            }


        }


        [Microsoft.AspNetCore.Mvc.HttpGet]
        public IHttpActionResult GetTableWorkorder_view(int page = 1, int pageSize = 10, string FBillno = null)
        {
            var context = new YourDbContext();

            IQueryable<sli_work_orders_view> query = context.Sli_work_orders_view;

            if (!string.IsNullOrEmpty(FBillno))
            {
                query = query.Where(q => q.Fwobillno.Contains(FBillno));
            }

            var totalCount = query.Count();
            var totalPages = (int)Math.Ceiling((double)totalCount / pageSize);
            var paginatedQuery = query.OrderByDescending(d => d.Fwoid).Skip((page - 1) * pageSize).Take(pageSize);
         

            var result = paginatedQuery.Select(a => new
            {
                Fwoid = a.Fwoid,
                Fwobillno = a.Fwobillno,
                Forderno = a.Forderno,
                Fnotes = a.Fnotes,
                Fproductno = a.Fproductno,
                Fwodate = a.Fwodate,
                Fqty = a.Fqty,
                Fwoweight = a.Fwoweight,
                Fwoplanstart = a.Fwoplanstart,
                Fwoplanend = a.Fwoplanend,
                Fwoordertype = a.Fwoordertype,
                Fwoentryqty = a.Fwoentryqty ?? 0,
                Fwoecommitqty = a.Fwoecommitqty ?? 0,
                Fwoestatus = a.Fwoestatus ?? 0,
                Fwoeclosed = a.Fwoeclosed ?? 0,
                Fworkorderlistid = a.Fworkorderlistid,
                Fworkqty = a.Fworkqty,
                Fworkweight = a.Fworkweight,
                Forderbillno = a.Forderbillno,
                Fplandeleliverydate = a.Fplandeleliverydate,
                Fslitemperatureend = a.Fslitemperatureend,
                Fcustid = a.Fcustid,
                Fcustno = a.Fcustno,
                Fcustname = a.Fcustname,
                Fcustomer = a.Fcustomer,
                Fid = a.Fid,
                Fentryid = a.Fentryid,
                Fseq = a.Fseq,
                // --注意这里可能是 Fsliheight 的拼写错误 
                Forderqty = a.Forderqty,
                Fnote = a.Fnote,
                Fplandeliverydate = a.Fplandeliverydate,
                Fmaterialid = a.Fmaterialid,
                Fnumber = a.Fnumber,
                Fname = a.Fname,
                Fdescription = a.Fdescription,
                Fsliouterdiameter = a.Fsliouterdiameter,
                Fslihight = a.Fslihight,
                Fsliallowanceod = a.Fsliallowanceod,
                Fsliallowanceid = a.Fsliallowanceid,
                Fsliallowanceh = a.Fsliallowanceh,
                //--注意这里可能是 Fsliblankmodel 的拼写错误
                Fsliweightmaterial = a.Fsliweightmaterial,
                Fsliweightforging = a.Fsliweightforging,
                Fsliweightgoods = a.Fsliweightgoods,
                Fslidrawingno = a.Fslidrawingno,
                Fslimetal = a.Fslimetal,
                Fsligoodsstatus = a.Fsligoodsstatus,
                Fsliprocessing = a.Fsliprocessing,
                Fslidelivery = a.Fslidelivery,
                Fsliblankmodel = a.Fsliblankmodel,
                Fslipunching = a.Fslipunching,
                Fslitemperaturebegin = a.Fslitemperaturebegin,
                Fslimould = a.Fslimould,
                Fsliroller = a.Fsliroller,
                Fsliheatingtimes = a.Fsliheatingtimes,
                Fsligrade = a.Fsligrade,
                Fsumnumber = a.Fsumnumber,
                Fsoqty = a.Fsoqty,
                Fwoqty = a.Fwoqty,
                Fwpqty = a.Fwpqty,
                Ffinisthqty = a.Ffinisthqty,
                Fstockqty = a.Fstockqty,
                Foption = a.Foption,
                Fobject = a.Fobject,
                Fmo = a.Fmo,
                Fworkorderliststatus = a.Fworkorderliststatus,
                Fpause = a.Fpause,
                Fcancel = a.Fcancel,
                // 添加其他需要的字段
            }).ToList();
            var response = new    // 定义 前端返回数据  总记录，总页，当前页 ，size,返回记录
            {
                code = 200,
                msg = "OK",
                data = new
                {
                    totalCounts = totalCount,
                    totalPagess = totalPages,
                    currentPages = page,
                    pageSizes = pageSize,
                    data = result
                }


            };

            return Ok(response);
        }
    }
}